﻿using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using System;
using System.Threading.Tasks;
using X23.Application.BillManagement;
using X23.Application.BillManagement.Dto;
using X23.Util.LinqUtil;
using X23.Model.Bill;
using System.Collections.Generic;

namespace X23.Controllers
{
    /// <summary>
    /// 账单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BillController : BaseApiController
    {
        private readonly IBillManagementService _billManagementService;

        public BillController(IBillManagementService billManagementService)
        {
            _billManagementService = billManagementService;
        }

        /// <summary>
        /// 查询账单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageData<BillDto>> QueryData(QueryDto input)
        {
            Expression<Func<Bill, bool>> predicate = x => x.UserId == CurrentUser.Id;
            var result = await _billManagementService.GetPageDataAsync(predicate, input.Pages, input.Rows, input.AdvancedSort);
            return result;
        }
        /// <summary>
        /// 获取指定年月的花销
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BillAmountDto> GetBillByTime(BillAmountInputDto input)
        {
            return await _billManagementService.GetBillByTime(input);
        }
        /// <summary>
        /// 获取分组账单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<BillGroupDto>> GetBillGroupList(BillGroupInputDto input)
        {
            return await _billManagementService.GetBillGroupList(input);
        }

        /// <summary>
        /// 添加账单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BillDto> Add(BillDto bill)
        {
            ResolveBillTime(bill);
            bill.UserId = CurrentUser.Id;
            return await _billManagementService.AddAsync(bill);
        }
        /// <summary>
        /// 修改账单
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BillDto> Update(BillDto bill)
        {
            ResolveBillTime(bill);
            await Verify(bill.Id);
            bill.UserId = CurrentUser.Id;
            return await _billManagementService.UpdateAsync(bill);
        }
        /// <summary>
        /// 删除账单
        /// </summary>
        /// <param name="id">账单id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> Delete(int id)
        {
            await Verify(id);
            return await _billManagementService.DeleteAsync(id);
        }


        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="id">账单id</param>
        /// <returns></returns>
        private async Task Verify(int id)
        {
            var bill = await _billManagementService.GetByIdAsync(id);
            if (bill.UserId != CurrentUser.Id)
            {
                throw new Exception("只能删除自己的账单！");
            }
        }

        /// <summary>
        /// 账单时间分解
        /// </summary>
        /// <param name="billDto"></param>
        private static void ResolveBillTime(BillDto billDto)
        {
            billDto.Year = billDto.DateTime.Year;
            billDto.Month = billDto.DateTime.Month;
            billDto.Day = billDto.DateTime.Day;
        }
    }
}
